#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

#include "button.h"

#include "flexible_button.h"


typedef enum
{
    USER_BUTTON_0 = 0,

    USER_BUTTON_MAX
} user_button_t;

flex_button_t user_button[USER_BUTTON_MAX];

extern osMessageQueueId_t key_queueHandle;
static void btn_0_cb(flex_button_t *btn)
{
    uint16_t key_value = 0;
    switch (btn->event)
    {
        case FLEX_BTN_PRESS_DOWN:

            break;
        case FLEX_BTN_PRESS_CLICK:
            key_value = 1;
            osMessageQueuePut(key_queueHandle, &key_value, NULL, 0);
            break;
        case FLEX_BTN_PRESS_LONG_START:
            key_value = 5;
            osMessageQueuePut(key_queueHandle, &key_value, NULL, 0);
            break;
        
        default:
            break;
    }
}

static uint8_t button_key0_read(void)
{
    return HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin);
}

void user_button_init(void)
{
    int i;
    
    memset(&user_button[0], 0x0, sizeof(user_button));

    user_button[USER_BUTTON_0].usr_button_read = button_key0_read;
    user_button[USER_BUTTON_0].cb = (flex_button_response_callback)btn_0_cb;

    for (i = 0; i < USER_BUTTON_MAX; i ++)
    {
        user_button[i].pressed_logic_level = LOGIC_LEVEL;
        user_button[i].click_start_tick = CLICK_START_TICK;
        user_button[i].short_press_start_tick = SHORT_PRESS_START_TICK;
        user_button[i].long_press_start_tick = LONG_PRESS_START_TICK;
        user_button[i].long_hold_start_tick = LONG_HOLD_START_TICK;

        flex_button_register(&user_button[i]);
    }
}

/**
 * @brief 处理按钮事件
 * 
 * 本函数负责调用flex_button_scan函数来扫描和处理按钮的按下和释放事件。
 * 它是按钮事件处理的入口函数，通过定期调用此函数来更新按钮状态。
 * 
 * 注意：此函数没有参数和返回值。
 */
void button_proc(void)
{
    flex_button_scan();
}





